<template>
  <div class="page-container">
    <div class="operators">
      <el-button type="primary" size="medium" @click="searchHandle"
        >刷新</el-button
      >
      <el-button type="primary" size="medium" @click="editHandle"
        >新增</el-button
      >
    </div>

    <el-table
      :data="tableData"
      :height="tableHeight"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      align="center"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" align="center" label="名称">
      </el-table-column>
      <el-table-column prop="remark" align="center" label="备注">
        <template slot-scope="scope">{{ scope.row.remark || "---" }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            @click="deleteHandle(scope.row.id)"
            type="danger"
            size="small"
            >删除</el-button
          >
          <el-button @click="editHandle(scope.row)" type="primary" size="small"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchParams.currentPage"
        :page-sizes="[10, 20, 50, 80]"
        :page-size="searchParams.pageSize"
        :total="count"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </div>

    <Edit
      title="角色"
      :formData="currentRole"
      :dialogVisible="dialogVisible"
      v-if="dialogVisible"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script>
import { listRole, deleteRole } from "@/api/organization/role";
import Edit from "./edit.vue";
import { autoTableHeight } from "@/utils/tools";
export default {
  data() {
    return {
      searchParams: {
        currentPage: 1,
        pageSize: 10,
      },

      tableData: [],
      count: 0,
      tableHeight: 600,

      currentRole: {},
      dialogVisible: false,
    };
  },
  components: {
    Edit,
  },
  created() {
    this.search();
    autoTableHeight(260, (e) => {
      this.tableHeight = e;
    });
  },
  methods: {
    searchHandle() {
      this.searchParams.currentPage = 1;
      this.search();
    },
    closeDialog(isRefresh) {
      this.dialogVisible = false;
      this.currentRole = {};
      isRefresh && this.search();
    },
    search() {
      listRole(this.searchParams).then((res) => {
        let { data, count } = res.data;
        this.count = count;
        this.tableData = data;
      });
    },
    deleteHandle(id) {
      this.$confirm(`确认删除当前角色?`, "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteRole({
            id,
          }).then((res) => {
            this.search();
          });
        })
        .catch(() => {});
    },
    editHandle(role) {
      if (role.id) {
        this.currentRole = role;
      }
      this.dialogVisible = true;
    },
    // 分页
    handleSizeChange(val) {
      if (this.tableData.length === 0) return;
      this.searchParams.pageSize = val;
      this.search();
    },
    handleCurrentChange(val) {
      this.searchParams.currentPage = val;
      this.search();
    },
  },
};
</script>

<style scoped lang="scss">
.pagination-container {
  text-align: right;
}
.operators {
  margin-bottom: 16px;
  padding: 10px 10px 10px 20px;
  background-color: #fff;
  border-radius: 10px;
}
.el-cascader {
  width: 100%;
}
.dialog-btns {
  text-align: center;
}
</style>
